package goodroommate.ui.roommate;

import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import goodroommate.domain.User;
import goodroommate.domain.UserDetail;
import goodroommate.domain.UserDetail_UserInfoForKmeans;
import goodroommate.exception.DatabaseException;
import goodroommate.exception.ItemAlreadyExistsException;
import goodroommate.exception.ItemNotExistsException;
import goodroommate.service.IUserDetailService;
import goodroommate.service.IUserInfoService;
import goodroommate.service.impl.UserDetailService;
import goodroommate.service.impl.UserInfoService;

/**
 * Servlet implementation class FindRoommateServlet
 */
@WebServlet("/Roommate/GetUserGroupFromComputingPlatform")
public class GetUserGroupFromComputingPlatform extends HttpServlet {
	private static final long serialVersionUID = 1L;
    IUserInfoService userInfoService=new UserInfoService();
    IUserDetailService userDetailService=new UserDetailService();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetUserGroupFromComputingPlatform() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {

			ArrayList<UserDetail_UserInfoForKmeans> estimationresults = new ArrayList<>();
			ArrayList<UserDetail_UserInfoForKmeans>[] results = userInfoService
					.getUserGroupFromComputingPlatform(estimationresults);
			request.setAttribute("results", results);
			request.setAttribute("estimationresults", estimationresults);
			request.setAttribute("tag", "fromgroup");
			request.getRequestDispatcher("getusergroupfromcomputingplatform.jsp").forward(request, response);
			return;
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.sendError(500);
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if(!"userinfo".equals(action)){
			response.sendRedirect(request.getContextPath()+"/Roommate/FindRoommate");
			return;
		}
		HttpSession session=request.getSession();
		User user=(User)session.getAttribute("user");
		if(user==null){
			response.sendRedirect(request.getContextPath()+"/Account/Login");
			return ;
		}
		
		Time sleeptime=Time.valueOf(request.getParameter("sleeptime")+":00");
		Time weekuptime=Time.valueOf(request.getParameter("weekuptime")+":00");
		
		if(sleeptime==null||"".equals(sleeptime)
				||weekuptime==null||"".equals(weekuptime)){
			response.sendRedirect(request.getContextPath()+"/Roommate/FindRoommate");
			return ;
		}
		
		try{
			if(userInfoService.addUserInfo(user.getId(), sleeptime, weekuptime)){
				UserDetail userDetail=userDetailService.getUserDetailByUserID(user.getId());
				request.setAttribute("userDetail", userDetail);
				request.setAttribute("errorCode", 0);
				request.getRequestDispatcher("adduserinfo.jsp").forward(request, response);
				return;
			}	
		}catch(ItemAlreadyExistsException e){
			request.setAttribute("errorCode", 1);
			request.getRequestDispatcher("adduserinfo.jsp").forward(request, response);
			return;
		}catch(DatabaseException e){
			response.sendError(500);
			return;
		}catch(ItemNotExistsException e){
			request.setAttribute("userDetail", null);
			request.getRequestDispatcher("adduserinfo.jsp").forward(request, response);
			return;
		}
	}

}
